/**
 *Create time: 2023-03-31 13:11
 *Author: Andy
 *Description: 扫描二维码页面
 */
package com.andy.nature.ui.activity

import android.os.Bundle
import com.andy.common.extend.setOnIMClickListener
import com.andy.logcat.LogUtils
import com.andy.nature.R
import com.andy.nature.databinding.ActivityScanCodeBinding
import com.andy.qrcode.CaptureActivity
import com.andy.qrcode.ScanResultListener
import com.andy.toast.extend.showNormalToast

class ScanCodeActivity : CaptureActivity(), ScanResultListener {
    private lateinit var mBinding: ActivityScanCodeBinding
    private var isTorchOn = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityScanCodeBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        setScanResultListener(this)
        with(mBinding) {
            ivBack.setOnIMClickListener {
                onBackPressedDispatcher.onBackPressed()
            }
            ivLight.setOnIMClickListener {
                isTorchOn = if (isTorchOn) {
                    ivLight.setImageResource(R.drawable.ic_scan_light_off)
                    cameraManager.setTorch(false)
                    false
                } else {
                    ivLight.setImageResource(R.drawable.ic_scan_light_on)
                    cameraManager.setTorch(true)
                    true
                }
            }
        }
    }

    override fun ScanResult(result: String?) {
        result?.showNormalToast()
        LogUtils.e("Ancoding-------$result")
    }

    override fun ScanError() {
        "Scan error".showNormalToast()
        LogUtils.e("Ancoding-------Scan error")
    }

}